//
//  CQTitleTableViewSectionHeader.m
//  BiaoliApp
//
//  Created by qian on 2020/11/18.
//

#import "CQTitleTableViewSectionHeader.h"
#import <Masonry/Masonry.h>
#import <CJBaseUIKit/UIColor+CJHex.h>
#import <CJBaseHelper/NSObjectCJHelper.h>
#import <CJContainer/CQHorizontalImageLabelView.h>

//#import <CQThemeBaseUIKit/CQPromptLabel.h>

@interface CQTitleTableViewSectionHeader () {
    
}
//@property (nonatomic, strong) UIImageView *imageView;
//@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) CQHorizontalImageLabelView *titleLabel;


@end

@implementation CQTitleTableViewSectionHeader

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.backgroundColor = CJColorFromHexString(@"#F7F7F8");
    
    self.titleLabel = [[CQHorizontalImageLabelView alloc] initWithIconHeight:22 iconTitleSpacing:10 contentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    self.titleLabel.titleLable.textColor = CJColorFromHexString(@"#0C101B");
    self.titleLabel.titleLable.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [self addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(26);
        make.height.mas_equalTo(22);
        make.bottom.equalTo(self).offset(-20);
    }];

}


#pragma mark - Event
/*
 *  设置title标题和prompt提示
 *
 *  @param image                标题对应的图片
 *  @param title                标题
 *  @param prompt               标题对应的提示
 *  @param isBlack              这题颜色
 */
- (void)setupImage:(UIImage *)image
             title:(NSString *)title
            prompt:(NSString *)prompt
         themeType:(BOOL)isBlack
{
    self.titleLabel.imageView.image = image;
    
    NSString *prefixText = title;
    NSString *suffixText = prompt;
    
    self.backgroundColor = CJColorFromHexString(@"#F7F7F8");
    UIColor *prefixTextColor = CJColorFromHexString(@"#0C101B");
    UIColor *suffixTextColor = CJColorFromHexString(@"#0C101B");
    if (isBlack) {
        self.backgroundColor = CJColorFromHexString(@"#26262C");
        prefixTextColor = CJColorFromHexString(@"#6D6F76");
        suffixTextColor = CJColorFromHexString(@"#6D6F76");
    }
    
    self.titleLabel.titleLable.attributedText = header_promptAttributedText(prefixText, prefixTextColor, suffixText, suffixTextColor);
}

/*
 *  分别对字符串prefixText和suffixText进行富文本的设置(eg:上传健康证（至少要一张健康证照片）)
 *
 *  @param prefixText               开头字符串
 *  @param suffixText               尾部字符串
 *  @param suffixTextFont           尾部字符串字体颜色
 *  @param suffixTextColor          尾部字符串字体大小
 *
 *  @return 符合要求的富文本
 */
NSAttributedString *header_promptAttributedText(NSString * _Nonnull prefixText, UIColor *prefixTextColor, NSString *_Nullable suffixText, UIColor *suffixTextColor) {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.lineSpacing = 3;
    paragraphStyle.firstLineHeadIndent = 0;             //段首行缩进
    paragraphStyle.alignment = NSTextAlignmentLeft;     //左对齐

    UIFont *prefixTextFont = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    NSDictionary *prefixAttributes = @{NSParagraphStyleAttributeName: paragraphStyle,
                                       NSFontAttributeName:           prefixTextFont,
                                       NSForegroundColorAttributeName:prefixTextColor
                                       };
    NSMutableAttributedString *prefixAttributedText = [[NSMutableAttributedString alloc] initWithString:prefixText attributes:prefixAttributes];



    NSMutableAttributedString *totalAttributedText = [[NSMutableAttributedString alloc] init];
    totalAttributedText = prefixAttributedText;

    if (!isEmptyObjectCJHelper(suffixText)) {
        UIFont *suffixTextFont = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
        NSDictionary *suffixAttributes = @{NSParagraphStyleAttributeName: paragraphStyle,
                                           NSFontAttributeName:           suffixTextFont,
                                           NSForegroundColorAttributeName:suffixTextColor
                                           };
        NSMutableAttributedString *suffixAttributedText = [[NSMutableAttributedString alloc] initWithString:suffixText attributes:suffixAttributes];

        [totalAttributedText appendAttributedString:suffixAttributedText];
    }

    return totalAttributedText;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
